package jmu.cze.controller;

import jmu.cze.pojo.Cabin;
import jmu.cze.pojo.OnFlight;
import jmu.cze.pojo.TicketType;
import jmu.cze.service.OnFlightService;
import jmu.cze.utl.ResultUtil;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Controller
@RequestMapping("/admin")
public class OnFlightController {
    private OnFlightService onFlightService;
    @Resource(name = "onFlightServiceImpl")
    public void setOnFlightService(OnFlightService onFlightService) {
        this.onFlightService = onFlightService;
    }

    @GetMapping("/onFlight/{id}")
    public String flight(@PathVariable("id") int id,Model model){
        model.addAttribute("onFlights", onFlightService.selectOnFlightByFlightID(id));
        model.addAttribute("cabinMap",  onFlightService.selectAllCabins());
        return "onFlight";
    }

    @GetMapping("/ticketType/{id}")
    @ResponseBody
    public String ticketType(@PathVariable("id") int id){
        return ResultUtil.getJson(1, "ticketTypes",onFlightService.selectTicketTypeByOnFlightID(id));
    }

    @PostMapping("/addOnFlight")
    @ResponseBody
    public String addOnFlight(OnFlight onFlight){
        return ResultUtil.getResult(onFlightService.insertOnFlight(onFlight));
    }

    @PostMapping("/updateOnFlight")
    @ResponseBody
    public String updateOnFlight(OnFlight OnFlight){
        return ResultUtil.getResult(onFlightService.updateOnFlight(OnFlight));
    }

    @GetMapping("/deleteOnFlight/{id}")
    @ResponseBody
    public String deleteOnFlight(@PathVariable("id") int id){
        return ResultUtil.getResult(onFlightService.deleteOnFlightByID(id));
    }

    @PostMapping("/addCabin")
    @ResponseBody
    public String addCabin(Cabin cabin){
        return ResultUtil.getResult(onFlightService.insertCabin(cabin));
    }

    @PostMapping("/updateCabin")
    @ResponseBody
    public String updateCabin(Cabin Cabin){
        return ResultUtil.getResult(onFlightService.updateCabin(Cabin));
    }

    @GetMapping("/deleteCabin/{id}")
    @ResponseBody
    public String deleteCabin(@PathVariable("id") int id){
        return ResultUtil.getResult(onFlightService.deleteCabinByID(id));
    }

    @PostMapping("/addTicketType")
    @ResponseBody
    public String addTicketType(TicketType ticketType){
        return ResultUtil.getResult(onFlightService.insertTicketType(ticketType));
    }

    @PostMapping("/updateTicketType")
    @ResponseBody
    public String updateTicketType(TicketType ticketType) {
        return ResultUtil.getResult(onFlightService.updateTicketType(ticketType));
    }

    @GetMapping("/deleteTicketType/{id}")
    @ResponseBody
    public String deleteTicketType(@PathVariable("id") int id){
        return ResultUtil.getResult(onFlightService.deleteTicketTypeByID(id));
    }
}
